package cn.bdqn.sys.service.impl;

import cn.bdqn.model.ConstUtil;
import cn.bdqn.model.EUList;
import cn.bdqn.model.PageBean;
import cn.bdqn.model.R;
import cn.bdqn.service.BaseService;
import cn.bdqn.sys.mapper.EnterpriseMapper;
import cn.bdqn.sys.mapper.UserMapper;
import cn.bdqn.sys.mapper.UserRoleMapper;
import cn.bdqn.sys.model.Enterprise;
import cn.bdqn.sys.model.EnterpriseExample;
import cn.bdqn.sys.model.User;
import cn.bdqn.sys.model.UserRole;
import cn.bdqn.sys.service.EnterpriseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
/**
 * @author gurilla
 * @create 2019-08-03 10:44
 */
@Service
public class EnterpriseServiceImpl  extends BaseService implements EnterpriseService{

    @Resource
    private EnterpriseMapper enterpriseMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public EUList list(PageBean pagebean, String name) {
        EnterpriseExample enterpriseExample=new EnterpriseExample();
        EnterpriseExample.Criteria criteria = enterpriseExample.createCriteria();
        criteria.andEidNotEqualTo(ConstUtil.WYBID);
        criteria.andIsdelEqualTo(false);
        if(!StringUtils.isEmpty(name)){
            criteria.andEnameLike("%"+name+"%");
        }
        PageHelper.startPage(pagebean.getPage(),pagebean.getRows());
        List<Enterprise> enterprises = enterpriseMapper.selectByExample(enterpriseExample);
        PageInfo<Enterprise> info = new PageInfo<>(enterprises);
        EUList<Enterprise> el = new EUList<>(info.getTotal(),info.getList());
        return el;
    }

    @Transactional
    @Override
    public R add(Enterprise enterprise) {
//        try {
            //1.插入企业信息
            enterprise.setOpid(getUser().getAccount());
            enterpriseMapper.insertSelective(enterprise);
            //2.生成企业管理员帐号
            User user = new User();
            user.setIsadmin(true);
            user.setEid(enterprise.getEid());
            user.setAccount((enterprise.getEcode()+"_admin").toUpperCase());

            userMapper.insertSelective(user);
            //3.给企业管理员赋权限，除了企业管理的权限外的所有权限;需要插两张表，user-role和role-resources
            UserRole userRole = new UserRole();
            userRole.setEid((long) enterprise.getEid());
            userRole.setUserId(user.getUid().longValue());
            userRole.setRoleId((long) ConstUtil.ADMIN);
            userRoleMapper.insertSelective(userRole);
            return R.ok();
//        }catch (Exception e){
//            e.printStackTrace();
//            return R.error("增加企业信息失败!");
//        }

    }

    @Override
    public R delete(Integer eid) {
        Enterprise enterprise = new Enterprise();
        enterprise.setEid(eid);
        enterprise.setIsdel(true);
        enterpriseMapper.updateByPrimaryKeySelective(enterprise);
        return R.ok();
    }
}
